import UIKit
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
extension RCTRootView {
    @objc func disableActivityIndicatorAutoHide(_ disable: Bool) {
        // 递归查找并修改所有 UIActivityIndicatorView
        func findAndModifyIndicator(in view: UIView) {
            for subview in view.subviews {
                if let indicator = subview as? UIActivityIndicatorView {
                    indicator.hidesWhenStopped = !disable
                }
                findAndModifyIndicator(in: subview)
            }
        }
        
        findAndModifyIndicator(in: self)
    }
}
@main
class AppDelegate: RCTAppDelegate {
  
  override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    self.moduleName = "tsun smart pro"
    self.dependencyProvider = RCTAppDependencyProvider()
    self.initialProps = [:]
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
  override func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
          return Orientation.getOrientation()
  }
    
  override func sourceURL(for bridge: RCTBridge) -> URL? {
    self.bundleURL()
  }
  
  override func customize(_ rootView: RCTRootView!) {
      super.customize(rootView)
      let launchVC = LaunchViewController()
      let launchView = launchVC.view!
      launchView.frame = rootView.bounds
      launchView.tag = 9999
      let appProperties: [String: Any] = [
        "onAnimationComplete":false
      ]
      launchVC.onAnimationComplete = { [weak rootView] in
          DispatchQueue.main.async {
              guard let rootView = rootView else { return }
              var updatedProps = rootView.appProperties ?? [:]
              updatedProps["onAnimationComplete"] = true
              rootView.appProperties = updatedProps
          }
      }
      rootView.appProperties=appProperties
      rootView.disableActivityIndicatorAutoHide(true)
      rootView.loadingView = launchView
  }

  override func bundleURL() -> URL? {
          #if DEBUG
              RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
          #else
              OtaHotUpdate.getBundle()  // -> Add this line
          #endif
  }
}
